﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace zijian666.Data
{
    internal class DiagnosticData : DynamicRecord
    {
        private static readonly Dictionary<string, int> _indexer = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
        {
            [nameof(Name)] = 0,
            [nameof(Content)] = 1,
            [nameof(Exception)] = 2,
            [nameof(File)] = 3,
            [nameof(Member)] = 4,
            [nameof(Line)] = 5,
        };
        public DiagnosticData(string name, string content, Exception ex, string file, string member, int line)
            : base(_indexer, new object[] { name, content, ex, file, member, line })
        {

        }

        private dynamic GetPropertyValue([CallerMemberName] string member = null) => this[member];

        public string Name => GetPropertyValue();
        public string Content => GetPropertyValue();
        public Exception Exception => GetPropertyValue();
        public string File => GetPropertyValue();
        public string Member => GetPropertyValue();
        public string Line => GetPropertyValue();

        public override string ToString()
        {
            if (Exception == null)
            {
                return $"{Name}: {Content}";
            }
            return $"{Name}: {Content}\n{File}#{Member}:{Line}\n{Exception}";
        }
    }
}
